package com.itheima.task;

import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 任务调度的配置类
 */
@Configuration
public class SampleSchduler {

    /**
     * 构建任务对象
     * @return
     */
    @Bean
    public JobDetail jobDetail(){
        //withIdentity给任务起id
        return JobBuilder.newJob(SampleJob.class).withIdentity("jobid").storeDurably().build();
    }

    //触发对象
    @Bean
    public Trigger trigger(){
        //方式1:   每2秒执行一次   循环执行
        SimpleScheduleBuilder ssb = SimpleScheduleBuilder
                .simpleSchedule()
                .withIntervalInSeconds(2)
                .repeatForever();

        //方式2： cron  每秒执行一次   循环执行
        CronScheduleBuilder csb = CronScheduleBuilder.cronSchedule("* * * * * ?");

        return TriggerBuilder.newTrigger()
                .forJob(jobDetail())
                .withIdentity("triggerid")
                .withSchedule(csb)
                .build();
    }

}
